﻿/***************************************************************************
 *		ElGee Google Chart API Helper
 *		Copyright(C) 2011 by Laurent Geffroy lgeffroy@gmail.com
 * 
 *		This program is free software; you can redistribute it and/or 
 *		modify it under the terms of the GNU General Public License as
 *		published by the Free Software Foundation; either version 2 of 
 *		the License, or (at your option) any later version.
 *		
 *		This program is distributed WITHOUT ANY WARRANTY; without even the
 *		implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
 *		See the GNU General Public License for more details.
 *	
 *      For particular licensing, please contact me at lgeffroy@gmail.com
 *****************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;

namespace ElGee.Google.Charts
{
    /// <summary>
    /// Chart Style Class
    /// </summary>
    public class ChartStyle : BaseChartStyle
    {
        private readonly string[] _fontNameExclusion = {"arial"};

        /// <summary>
        /// Gets or sets the color of the background.
        /// </summary>
        /// <value>background</value>
        public ChartBackground BackgroundColor { get; set; }


        /// <summary>
        /// Gets or sets the chart area.
        /// An object with members to configure the placement and size of the chart area (where the chart itself is drawn, excluding axis and legends). Two formats are supported: a number, or a number followed by %. A simple number is a value in pixels; a number followed by % is a percentage
        /// </summary>
        /// <value>chartArea</value>
        public Area ChartArea { get; set; }

        /// <summary>
        /// Gets or sets the font.
        /// The default font size, in pixels, of all text in the chart. You can override this using properties for specific chart elements.
        /// The default font face for all text in the chart. You can override this using properties for specific chart elements.
        /// </summary>
        /// <value>fontSize</value>
        public FontBase Font { get; set; }

        /// <summary>
        /// Gets or sets the legend.
        /// Position of the legend
        /// </summary>
        /// <value>legend</value>
        public LegendPosition Legend { get; set; }

        /// <summary>
        /// Gets or sets the legend text style.
        /// An object that specifies the legend text style. The object has this format: {color: <string>, fontName: <string>, fontSize: <number>}
        /// </summary>
        /// <value>legendTextStyle</value>
        public FontWithColor LegendTextStyle { get; set; }

        /// <summary>
        /// Gets or sets the title.
        /// Text to display above the chart
        /// </summary>
        /// <value>title</value>
        public string Title { get; set; }

        /// <summary>
        /// Gets or sets the title text style.
        /// An object that specifies the title text style. The object has this format: {color: <string>, fontName: <string>, fontSize: <number>}
        /// </summary>
        /// <value>The title text style.</value>
        public FontWithColor TitleTextStyle { get; set; }

        /// <summary>
        /// Gets or sets the tooltip text style.
        /// An object that specifies the tooltip text style. The object has this format: {color: <string>, fontName: <string>, fontSize: <number>}
        /// </summary>
        /// <value>The tooltip text style.</value>
        public FontWithColor TooltipTextStyle { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether [reverse categories].
        /// If set to true, will draw series from right to left. The default is to draw left-to-right.
        /// </summary>
        /// <value>reverseCategories</value>
        public bool ReverseCategories { get; set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="ChartStyle"/> class.
        /// </summary>
        public ChartStyle()
        {
            Colors = new List<string>();
            BackgroundColor = new ChartBackground();
            ChartArea = new Area();
            Font = new FontBase();
            Legend = LegendPosition.Undefined;
            LegendTextStyle = new FontWithColor();
            TitleTextStyle = new FontWithColor();
            TooltipTextStyle = new FontWithColor();
            Height = 0;
            Width = 0;
            ReverseCategories = false;
        }

        /// <summary>
        /// Returns a <see cref="System.String"/> that represents this instance.
        /// </summary>
        /// <returns>
        /// A <see cref="System.String"/> that represents this instance.
        /// </returns>
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();

            string baseParams = base.ToString();
            if (!String.IsNullOrWhiteSpace(baseParams))
                sb.Append(base.ToString()).Append(",");

            if ((BackgroundColor != null) && (!BackgroundColor.IsEmpty))
                sb.Append(BackgroundColor.ToString()).Append(",");

            if ((ChartArea != null) && (!BackgroundColor.IsEmpty))
                sb.Append(ChartArea.ToString()).Append(",");

            if (Font != null)
            {
                // Add the font size if not 0
                if (Font.FontSize > 0)
                    sb.Append("fontSize: ").Append(Font.FontSize).Append(",");

                // Add the font name if not empty or different of Arial
                if ((!String.IsNullOrWhiteSpace(Font.FontName)) && (Array.IndexOf(_fontNameExclusion, Font.FontName.ToLower()) < 0))
                    sb.Append("fontName: '").Append(SecureString(Font.FontName)).Append("',");
            }


            // Legend Position
            if ((Legend != LegendPosition.Undefined) && (Legend != LegendPosition.Right))
                sb.Append("legend: '").Append(Legend.ToString().ToLower()).Append("',");

            if (LegendTextStyle != null)
            {
                if (!LegendTextStyle.IsEmpty)
                    sb.Append("legendTextStyle: {").Append(LegendTextStyle.ToString()).Append("},");
            }

            // The Title
            if (!String.IsNullOrWhiteSpace(Title))
                sb.Append("title: '").Append(SecureString(Title)).Append("',");

            // Title Text Style
            if (TitleTextStyle != null)
            {
                if (!TitleTextStyle.IsEmpty)
                    sb.Append("titleTextStyle: {").Append(TitleTextStyle.ToString()).Append("},");
            }

            if (TooltipTextStyle != null)
            {
                if (!TooltipTextStyle.IsEmpty)
                    sb.Append("tooltipTextStyle: {").Append(TooltipTextStyle.ToString()).Append("},");
            }

            if (ReverseCategories)
                sb.Append("ReverseCategories: true,");

            // Removing the last comma
            sb.Remove(sb.Length - 1, 1);

            return sb.ToString();
        }
    }

}
